package com.wn.class30.service.impl;

import com.wn.class30.entity.Shoppingcar;
import com.wn.class30.entity.ShoppingcarExample;
import com.wn.class30.mapper.ShoppingcarDao;
import com.wn.class30.service.ShoppingCarService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Resource
    ShoppingcarDao shoppingcarDao;

    @Override
    public Shoppingcar getOneProduct(Shoppingcar shoppingcar) {
        ShoppingcarExample example = new ShoppingcarExample();
        ShoppingcarExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(shoppingcar.getUserId());
        criteria.andProductIdEqualTo(shoppingcar.getProductId());
        List<Shoppingcar> shoppingcars = shoppingcarDao.selectByExample(example);
        System.out.println("........."+shoppingcars);
        if(shoppingcars.size() != 0){
            return shoppingcars.get(0);
        }
        return null;
    }

    @Override
    public Integer insertProduct(Shoppingcar shoppingcar) {
        return shoppingcarDao.insert(shoppingcar);
    }

    @Override
    public List<Map<String, Object>> getBusinessByUserId(Integer userid) {
        return shoppingcarDao.getBusinessByUserId(userid);
    }

    @Override
    public List<Map<String, Object>> getProductByUserId(Integer userid) {
        return shoppingcarDao.getProductByUserId(userid);
    }

    @Override
    public Integer updateProductNum(Shoppingcar shoppingcar) {
        ShoppingcarExample example = new ShoppingcarExample();
        ShoppingcarExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(shoppingcar.getProductId());
        int i = shoppingcarDao.updateByExampleSelective(shoppingcar, example);
        return i;
    }

    @Override
    public Integer deleteCarProduct(Integer shoppingcarId) {
        return shoppingcarDao.deleteByPrimaryKey(shoppingcarId);
    }
}
